﻿Class MainWindow
    Private Sub MetroWindow_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        RefreshList()
    End Sub

    Private Sub AddTile(Title As String, Icon As System.Windows.Media.Visual, Optional IsAddButton As Boolean = False)
        Dim myTile As New MahApps.Metro.Controls.Tile
        myTile.Title = Title
        myTile.Content = New MahApps.Metro.Controls.MetroImage() With {.Source = Icon}
        If Not IsAddButton Then
            AddHandler myTile.Click, AddressOf Tile_Click
        Else
            AddHandler myTile.Click, AddressOf AddTile_Click
        End If
        myWrapPanel.Children.Add(myTile)
    End Sub

    Private Sub Tile_Click(ByVal sender As MahApps.Metro.Controls.Tile, e As EventArgs)
        If Keyboard.IsKeyDown(Key.Delete) Then
            If MsgBox("Remove this account from list?", vbYesNo) = MsgBoxResult.Yes Then
                SaveHelper.RemoveAcc(sender.Title)
                RefreshList()
            End If
        Else
            SaveHelper.SwitchCurrentAcc(sender.Title)
            Application.Current.Shutdown()
        End If
    End Sub

    Private Sub AddTile_Click(ByVal sender As MahApps.Metro.Controls.Tile, e As EventArgs)
        Dim myAddCurrentUserWindow As New AddCurrentUserWindow
        myAddCurrentUserWindow.ShowDialog()
        RefreshList()
    End Sub

    Private Sub RefreshList()
        myWrapPanel.Children.Clear()
        Dim myUsers As String() = SaveHelper.LoadSavedAccs
        For N = 0 To myUsers.Length - 1
            AddTile(myUsers(N), CType(Me.Resources("appbar_smiley_" & (N Mod 10)), System.Windows.Media.Visual))
        Next
        AddTile("Add", CType(Me.Resources("appbar_add"), System.Windows.Media.Visual), True)
    End Sub

    Private Sub WindowCommandAbout_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles WindowCommandAbout.Click
        Dim myAboutWindow As New AboutWindow
        myAboutWindow.ShowDialog()
    End Sub
End Class
